package com.qualysoft.checkin;


import com.qualysoft.Human;
import com.qualysoft.JavaNinja;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.util.ArrayList;
import java.util.List;

public class QualysoftChecker {

    public void checkYourself(Human candidate) {
        System.out.println("========================================================================================");
        if (candidate instanceof JavaNinja) {

            System.out.println("Dude, your java rocks! :) Submitting your data...");

            boolean success = true;
            DefaultHttpClient httpclient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("http://www.qualysoft.com/checkin");
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("name", candidate.getName()));
            nvps.add(new BasicNameValuePair("email", candidate.getEmail()));
            nvps.add(new BasicNameValuePair("phone", candidate.getPhone()));
            try {
                httpPost.setEntity(new UrlEncodedFormEntity(nvps));
                HttpResponse response2 = httpclient.execute(httpPost);
                HttpEntity entity2 = response2.getEntity();
                EntityUtils.consume(entity2);
                if (response2.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
                    success = false;
                }
            } catch (Exception e) {
                System.err.println("Error checking in :(");
                System.err.println(e.getMessage());
                success = false;
            } finally {
                httpPost.releaseConnection();
            }
            if (!success) {
                System.out.println("========================================================================================");
                System.out.println("");
                System.out.println("Checkin failed! If you are interested in a Java Developer position at our company, please");
                System.out.println(" - contact Agnes per mail: agnes.torok@qualysoft.com");
                System.out.println(" - or call: +36203998883");
                System.out.println("");
                System.out.println("========================================================================================");
            }
        } else {
            System.out.println("Sorry, you are not  JavaNinja.");
        }
        System.out.println("========================================================================================");
    }

}
